package com.taikang.hmp.hbusiness.diabetes.measure.family.service.impl;

import java.util.List;

import com.taikang.framework.common.util.IDHelper;
import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.service.impl.BaseServiceImpl;
import com.taikang.hmp.hbusiness.diabetes.measure.family.service.IFamilyService;

/** 
 * 家人管理
 * author:qrp
 * time:2015年12月17日 下午3:05:11
 */
public class FamilyServiceImpl extends BaseServiceImpl implements IFamilyService{

	public List<Dto> getFriendList(Dto paramDto) {
		Dto dto = new BaseDto();
		String uid =  paramDto.getAsString("uid");
		if(paramDto.getAsString("ftype").equals("3")){//全部
			dto.put("ftype", "");
			dto.put("state", "");
			dto.put("self", "");
			dto.put("friend", uid);
		}else if(paramDto.getAsString("ftype").equals("2")){//新朋友，包括新的和旧的，当时为配合安卓需要
			dto.put("ftype", "1");
			dto.put("state", "");
			dto.put("self", "");
			dto.put("friend", uid);
		}else {
			dto.put("ftype", paramDto.getAsString("ftype"));
			dto.put("state", "1");
			dto.put("self", "");
			dto.put("friend", uid);
		}
		return super.getNewDao().queryForList("family_space.getFriendList", dto);
	}

	public String addFriend(Dto paramDto) {
		String uid = paramDto.getAsString("uid");
		String fmobile = paramDto.getAsString("fmobile");
		List<Dto> friendInfo = super.getNewDao().queryForList("family_space.getfriendinfo", fmobile);
		List<Dto> selfInfo = super.getNewDao().queryForList("family_space.getselfinfo", uid);
		if(friendInfo.size() > 0){
			String friendid = friendInfo.get(0).getAsString("user_id");
			if(this.isfriend(uid, friendid)){
				Dto dto = new BaseDto();		
				dto.put("id", IDHelper.genUUID());
				dto.put("uid", uid);		
				dto.put("user_name", selfInfo.get(0).getAsString("name"));
				dto.put("friend_name", friendInfo.get(0).getAsString("name"));
				dto.put("type", "1");//自己
				dto.put("state", "2");//初始化为新朋友
				dto.put("remark", paramDto.getAsString("remark"));
				dto.put("friend_id", friendInfo.get(0).getAsString("user_id"));
				super.getNewDao().insert("family_space.addFriend1", dto);
				Dto dto1 = new BaseDto();
				dto1.put("id", IDHelper.genUUID());
				dto1.put("uid", friendInfo.get(0).getAsString("user_id"));	
				dto1.put("user_name", friendInfo.get(0).getAsString("name"));
				dto1.put("friend_name", selfInfo.get(0).getAsString("name"));
				dto1.put("type", "0");//朋友
				dto1.put("state", "2");//初始化为新朋友
				dto1.put("remark", paramDto.getAsString("remark"));
				dto1.put("friend_id", uid);
				super.getNewDao().insert("family_space.addFriend1", dto1);
				return "success";
			}else{
				return "isfriend";
			}
		}else{
			return "friendnotexit";
		}
	}

	public void delFriend(Dto paramDto) {
		super.getNewDao().delete("family_space.delFriend", paramDto.getAsString("fuid"));
	}

	public void confirmFriend(Dto paramDto) {
		Dto dto = new BaseDto ();
		dto.put("state", paramDto.getAsString("state"));
		dto.put("fuid", paramDto.getAsString("fuid"));
		dto.put("uid", paramDto.getAsString("uid"));
		if(paramDto.getAsString("state").equals("1")){
			super.getNewDao().update("family_space.confirmFriend", dto);
		}else{
			super.getNewDao().delete("family_space.delFriend", paramDto.getAsString("fuid"));
		}
	}

	public boolean isfriend(String uid, String friendid) {
		Dto dto = new BaseDto();
		dto.put("uid", uid);
		dto.put("friendid", friendid);
		List<Dto> list = super.getNewDao().queryForList("family_space.isfriend", dto);
		if(list.size() == 0){
			return true;
		}else{
			return false;
		}
	}
}
